package com.ruoyi.web.controller.teaching;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.vo.PlatformEExamInVO;
import com.ruoyi.teaching.domain.vo.PlatformEExamQuestionInVO;
import com.ruoyi.teaching.service.PlatformEExamService;
import com.ruoyi.teaching.utils.UniversalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王旭
 * @since 2024-06-15
 */
@RestController
@RequestMapping("/platform/exam")
@Api(tags = "platform-考试管理")
public class PlatformEExamController {
    @Autowired
    private PlatformEExamService platformEExamService;

    /**
     * 查询老师自己发布的考试列表
     */
//    @PreAuthorize("@ss.hasPermi('platform:exam:list')")
    @ApiOperation(value = "查询老师自己发布的考试列表", notes = "查询老师自己发布的考试列表")
    @GetMapping("/list")
    public AjaxResult list(Integer pageNum, Integer pageSize, String examName, Integer examCourse,Integer deptId)
    {
        return platformEExamService.selectPlatformEExamList(pageNum,pageSize,examName,examCourse,deptId);
    }

//    /**
//     * 查询学生自己的考试列表
//     */
////    @PreAuthorize("@ss.hasPermi('platform:exam:list')")
//    @ApiOperation(value = "查询学生自己的考试列表", notes = "查询学生自己的考试列表")
//    @GetMapping("/listStudent")
//    public AjaxResult listStudent()
//    {
//        return platformEExamService.selectPlatformEExamListStudent();
//    }

    /**
     * 获取考试详细信息
     */
//    @PreAuthorize("@ss.hasPermi('platform:exam:query')")
    @ApiOperation(value = "获取考试详细信息", notes = "获取考试详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return platformEExamService.selectPlatformEExamById(id);
    }

    /**
     * 新增考试
     */
//    @PreAuthorize("@ss.hasPermi('platform:exam:add')")
    @Log(title = "新增考试", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增考试", notes = "新增考试")
    @PostMapping
    public AjaxResult add(@RequestBody PlatformEExamInVO platformEExamVO)
    {
        List<String> errorMessages = new ArrayList<>();
        errorMessages.addAll(UniversalUtil.validate(platformEExamVO.getExamName(),1,20,true,"考试名称"));
        errorMessages.addAll(UniversalUtil.validateNumeric(platformEExamVO.getExamTime(),"考试时长"));
        errorMessages.addAll(validateTotalValue(platformEExamVO));
        if(!CollectionUtils.isEmpty(errorMessages)){
            return AjaxResult.error(String.join(", ", errorMessages));
        }
        return platformEExamService.insertPlatformEExam(platformEExamVO);
    }


    /**
     * 删除考试
     */
//    @PreAuthorize("@ss.hasPermi('platform:exam:remove')")
    @Log(title = "删除考试", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除考试", notes = "删除考试")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return platformEExamService.deletePlatformEExamByIds(ids);
    }


    private List<String> validateTotalValue(PlatformEExamInVO platformEExamVO) {
        List<String> errorMessage = new ArrayList<>();
        if(platformEExamVO.getExamWay() == 0){
            int value = 0;
            for (PlatformEExamQuestionInVO platformEExamQuestion : platformEExamVO.getPlatformEExamQuestions()) {
                value += platformEExamQuestion.getQuestionValue();
            }
            if(value != 100){
                errorMessage.add("总分必须为100");
            }
        }else {
            int value = 0;
            for (PlatformEExamQuestionInVO platformEExamQuestion : platformEExamVO.getPlatformEExamQuestions()) {
                Integer questionValue = platformEExamQuestion.getQuestionValue();
                Integer questionNumber = platformEExamQuestion.getQuestionNumber();
                value += questionValue * questionNumber;
            }
            if(value!= 100){
                errorMessage.add("总分必须为100");
            }
        }
        return errorMessage;
    }

}

